//
//  FolderViewController.m
//  ViewTest
//
//  Created by Guest User on 11/29/13.
//  Copyright (c) 2013 Guest User. All rights reserved.
//

#import "FolderViewController.h"

@interface FolderViewController ()
@property (nonatomic, strong) NSString* uuid;
@property (nonatomic, strong) NSString* selectedDir;
@property (nonatomic, strong) NSMutableArray* dirs;
@property (nonatomic, strong) NSMutableArray* files;
@property (nonatomic, strong) NSString* theDir;

@end


@implementation FolderViewController

@synthesize theDir = _theDir;
@synthesize uuid = _uuid;

- (NSString *)theDir
{
    return _theDir ? _theDir : @"/";
}

- (void)setTheDir:(NSString *)theDir
{
    _theDir = theDir;
    self.navigationItem.title = theDir;
}

- (NSString *)uuid
{
    if (_uuid == nil) {
        _uuid = [[NSUUID UUID] UUIDString];
        NSLog(@"UUID: %@", _uuid);
    }
    return _uuid;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%@ %@", self.uuid, self.theDir);
    if (self.dirs == nil)
    {
        NSError* error = nil;
        NSArray* fileUrls = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:[NSURL fileURLWithPath:self.theDir] includingPropertiesForKeys:@[NSURLNameKey,  NSURLIsDirectoryKey] options:0 error:&error];
        if (error)
        {
            NSLog(@"%@", error);
        }
        
        self.dirs = [NSMutableArray array];
        self.files = [NSMutableArray array];
        
        for (NSURL* url in fileUrls) {
            NSString* path;
            [url getResourceValue:&path forKey:NSURLNameKey error:&error];
            
            NSNumber* isDir = nil;
            [url getResourceValue:&isDir forKey:NSURLIsDirectoryKey error:&error];
            if ([isDir boolValue]) {
                [self.dirs addObject:path];
            }
            else{
                [self.files addObject:path];
            }
        }
    }
    return section == 0 ? self.dirs.count : self.files.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* filename = indexPath.section == 0 ? self.dirs[indexPath.row] : self.files[indexPath.row];
    static NSString *cellIdentifier = @"name";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.imageView.image = indexPath.section == 0 ? [UIImage imageNamed:@"Folder"] : [UIImage imageNamed:@"Document"];
    cell.textLabel.text = filename;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedDir = indexPath.section == 0 ? self.dirs[indexPath.row] : self.files[indexPath.row];
    //NSLog(@"%@", self.segue);
    [self performSegueWithIdentifier:self.segue sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier]  isEqual: @"file"])
    {
        FolderViewController* vc = segue.destinationViewController;
        vc.theDir = [NSString pathWithComponents:@[self.theDir, self.selectedDir]];
    }
}

@end

